package com.king.cms.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.db.QueryPage;
import com.king.cms.log.LogService;
import com.king.cms.util.page.JpegTool;
import com.king.cms.common.*;
 

public class ClientAreaAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(ClientAreaAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
//		 
		 String action = request.getParameter("action");
		 if("list".equals(action)){
			 QueryPage query = new QueryPage(request,"clientarea.do?action=list","select  * from cms_client_area");
			 request.setAttribute("query", query);
			 return mapping.findForward("area.list");
		 }
		 if("save".equals(action)){
			 String areaId = request.getParameter("areaId");
			 String areaName = request.getParameter("areaName");
			 String areaFile = request.getParameter("imageFile");
			 String isSingle = request.getParameter("isSingle");
			 String saveName = request.getParameter("saveName");
			 if(null == areaId || "null".equals(areaId)){
				 this.getDBOperator(request).update("insert into cms_client_area (CLIENT_AREA_NAME,IS_SINGLE,IMAGE_NAME,IMAGE_SAVE_PATH) values(?,?,?,?)", new Object[]{areaName,isSingle,areaFile,saveName});
			 }else{
				 this.getDBOperator(request).update("update  cms_client_area set CLIENT_AREA_NAME=?,IS_SINGLE=?,IMAGE_NAME=?,IMAGE_SAVE_PATH=? where ID=?", new Object[]{areaName,isSingle,areaFile,saveName,areaId});
			 }
			 return new ActionForward("/clientarea.do?action=list");
		 }
		 if("view".equals(action)){
			 String areaId = request.getParameter("areaId");
			 if(null != areaId && !"null".equals(areaId)){
				 List area = this.getDBOperator(request).select("select  * from cms_client_area where id = ?", new Object[]{areaId});
				 Map data = (Map)area.get(0);
				 request.setAttribute("data", data); 
			 }
			 return mapping.findForward("area");
		 }
		 if("delete".equals(action)){
			 String[] areaId = request.getParameterValues("areaId");
			 if(null != areaId && areaId.length>0){
				 for(int i = 0;i<areaId.length;i++){
					 this.getDBOperator(request).update("delete from cms_client_area where id = ?", new Object[]{areaId[i]});
				 }
				
			 }
			 return new ActionForward("/clientarea.do?action=list");
		 }
		 if("upload".equals(action)){
			 try{
					
					String fileName = null; 
					FileUp fileUp = new FileUp(request);  
					List fileList = fileUp.getFileList();   
					if(null != fileList){
						FileRw f = new FileRw();
						for(int i = 0;i<fileList.size();i++){
							FileDoc file = (FileDoc)fileList.get(i);
							fileName = file.getFileName();
							String fileSaveName = file.getFileName();
							f.saveTempFile(file.getContent(), request.getRealPath("/")+FileRw.getFileSavePath(""),fileSaveName );
							try{ 

								JpegTool jpegTool = new JpegTool();  
								jpegTool.SetSmallWidth(800); 
								jpegTool.SetSmallHeight(600);

								jpegTool.doFinal(request.getRealPath("/")+FileRw.getFileSavePath("")+fileSaveName,request.getRealPath("/")+"//previewimage//"+fileSaveName); 

								}catch(Exception e){ 

								e.printStackTrace(); 

								}   
						} 
					}  
			 			request.setAttribute("fileName", fileName); 
			 			request.setAttribute("saveName", fileName); 
						return mapping.findForward("upload");
					 
				}catch(Exception e){
					e.printStackTrace();
				}
				 
			 
		 }
		 
		return null;
	 
	}
	 
}
